//
//  BanBu_choiceController.m
//  BanBu
//
//  Created by apple on 13-3-18.
//
//

#import "BanBu_choiceController.h"
#import "SVSegmentedControl.h"
#import "AppDataManager.h"
#import "BanBu_NavButton.h"
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"
#import "BanBu_NavigationController.h"
#import "BanBu_StarSearchViewController.h"
#import "UIViewController+Animation.h"
#import "BanBu_ApplyVipViewController.h"
#import "BanBu_VIPAlert.h"
@interface BanBu_choiceController ()

-(void)initSubViews;
-(void)toseleTime:(SVSegmentedControl*)sender;
-(void)changeToSelect:(UIButton *)sender;
@end
@interface SVSegmentedControl (fuck)

-(void)setMakeHeight:(float)height;

@end

@implementation SVSegmentedControl(fuck)


-(void)setMakeHeight:(float)height
{

    self.height=height;

}
@end



@implementation BanBu_choiceController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title=NSLocalizedString(@"selectTitle", nil);

    BanBu_NavButton *selectButton=[[BanBu_NavButton alloc] initWithButtonType:UIButtonCreatTypeBack];
    selectButton.position = ButtonPositionLeft;
    [selectButton addTarget:self action:@selector(toSelsect:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftButton = [[[UIBarButtonItem alloc] initWithCustomView:selectButton] autorelease];
    self.navigationItem.leftBarButtonItem = leftButton;
    [selectButton release];
    [self initSubViews];
    
    BanBu_ImageView *img = [[[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable] autorelease];
    self.view.backgroundColor = [UIColor colorWithPatternImage:img.image];
    
  self.view.backgroundColor=[UIColor colorWithRed:255.0/255 green:250.0/255 blue:240.0/255 alpha:1.0];
    
    
    
    
}

// 获取当前机器语言
-(NSString*)getPreferredLanguage
{
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString* preferredLang = [languages objectAtIndex:0];
    return preferredLang;
}

-(void)initSubViews
{
    NSString *str=NSLocalizedString(@"sexSelect", nil);
    
   CGSize titleSize = [str sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(1000, 30)];
    
    UILabel *userLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, titleSize.width, 20)];
    
    userLabel.text=NSLocalizedString(@"sexSelect", nil);
    
    userLabel.font=[UIFont systemFontOfSize:16];
    
    userLabel.backgroundColor=[UIColor clearColor];
    
    [self.view addSubview:userLabel];
    
    [userLabel release];
    
    // segment的title
   
    NSMutableDictionary *dic;
    
    if([UserDefaults valueForKey:[NSString stringWithFormat:@"%@electNearBy",MyAppDataManager.useruid]])
    {
        dic=[NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:[NSString stringWithFormat:@"%@electNearBy",MyAppDataManager.useruid]]];
        
    }else
    {
        dic=[NSMutableDictionary dictionaryWithCapacity:1];
        
    }

    NSArray *arr1 = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",NSLocalizedString(@"listTitle2",nil)],[NSString stringWithFormat:@"%@",NSLocalizedString(@"boyItem",nil)],[NSString stringWithFormat:@"%@",NSLocalizedString(@"girlItem", nil)],nil];
    segMent=[[SVSegmentedControl alloc]initWithSectionTitles:arr1];
    
    segMent.backgroundColor = [UIColor colorWithWhite:0 alpha:.2];
    segMent.crossFadeLabelsOnDrag = YES;
    [segMent.thumb setTintColor:[UIColor lightGrayColor]];
    if([dic count])
    {
        segMent.selectedIndex=[[dic valueForKey:@"sex"]intValue];
    }else{
        segMent.selectedIndex=0;
    }
    segMent.center=CGPointMake(160, 60);
    
    segMent.height=40;
    
    segMent.font=[UIFont systemFontOfSize:16];
    
    [segMent addTarget:self action:@selector(toseleSex:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:segMent];
    
    [segMent release];
    
    
    // 出现的时间
    
    NSString *time=NSLocalizedString(@"timeSelect", nil);
    
    CGSize size=[time sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(1000, 30)];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(15, 100, size.width, 20)];
    
    label.font=[UIFont systemFontOfSize:16];
    
    label.text=time;
    
    label.backgroundColor=[UIColor clearColor];
    
    [self.view addSubview:label];
    
    [label release];
    
     
    NSArray *timeArray = [NSArray arrayWithObjects:NSLocalizedString(@"oneHour", nil),NSLocalizedString(@"sixHour", nil),NSLocalizedString(@"oneDay", nil), NSLocalizedString(@"threeDay", nil),nil];

    
    segMentTime=[[SVSegmentedControl alloc]initWithSectionTitles:timeArray];
    
    segMentTime.crossFadeLabelsOnDrag = YES;
//    segMentTime.thumb.tintColor = [UIColor colorWithWhite:1 alpha:.3];
    segMentTime.backgroundColor = [UIColor colorWithWhite:0 alpha:.2];
    
    if([dic count])
    {
        segMentTime.selectedIndex=[[dic valueForKey:@"time"] intValue];
        
    }else{
    segMentTime.selectedIndex=0;
    }
    [segMent.thumb setTintColor:[UIColor lightGrayColor]];

    segMentTime.center=CGPointMake(160, 150);
    
    segMentTime.height=40;
    
    segMentTime.font=[UIFont systemFontOfSize:16];

    [segMentTime addTarget:self action:@selector(toseleTime:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:segMentTime];
    
    [segMentTime release];
    
    
    UILabel *messageLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 210, 240, 20)];
    
    messageLabel.backgroundColor=[UIColor clearColor];
    
    messageLabel.font=[UIFont systemFontOfSize:15];
    
    messageLabel.textAlignment=kTextAlignmentCenter;
    
   // messageLabel.text=@"选中时间内刷新过位置的用户";
    
    messageLabel.textColor=[UIColor darkGrayColor];
    [self.view addSubview:messageLabel];
    
    [messageLabel release];
    
    
    //////
    NSString *ageStr=NSLocalizedString(@"ageLabel", nil);
    
    CGSize size1 = [ageStr sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(1000, 30)];
    
    UILabel *ageLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 190, size1.width, 20)];
    
    ageLabel.font=[UIFont systemFontOfSize:16];
    
    ageLabel.text=ageStr;
    
    ageLabel.backgroundColor=[UIColor clearColor];
    
    [self.view addSubview:ageLabel];
    
    [ageLabel release];

    NSArray *ageArr = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",NSLocalizedString(@"listTitle2",nil)],[NSString stringWithFormat:@"%@",@"18-20"],[NSString stringWithFormat:@"%@",@"21-25"],[NSString stringWithFormat:@"%@",@"26-30"],[NSString stringWithFormat:@"%@",@" > 30"],nil];
    
    
    segMentAge=[[SVSegmentedControl alloc]initWithSectionTitles:ageArr];

    segMentAge.crossFadeLabelsOnDrag = YES;
    //    segMentTime.thumb.tintColor = [UIColor colorWithWhite:1 alpha:.3];
    segMentAge.backgroundColor = [UIColor colorWithWhite:0 alpha:.2];
    
    if([dic count])
    {
        segMentAge.selectedIndex=[[dic valueForKey:@"age"] intValue];
        
    }else{
        segMentAge.selectedIndex=0;
    }
    [segMentAge.thumb setTintColor:[UIColor lightGrayColor]];
    
    segMentAge.center=CGPointMake(160, 240);
    
    segMentAge.height=40;
    
    segMentAge.font=[UIFont systemFontOfSize:16];
    
    [segMentAge addTarget:self action:@selector(toSelectAge:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:segMentAge];
    
    [segMentAge release];
    if(![[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"userlevel"] integerValue]>0)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        segMentAge.userInteractionEnabled = NO;
        btn.backgroundColor = [UIColor clearColor];
        btn.frame = segMentAge.frame;
        [btn addTarget:self action:@selector(noticeVip) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }

    ///////
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(15, 280, 290, 50);
    CGSize size4 = [NSLocalizedString(@"starLabel", nil) sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(1000, 30)];
    
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, size4.width, 30)];
    lab.font=[UIFont systemFontOfSize:16];
    lab.text=NSLocalizedString(@"starLabel", nil);
//    btn.backgroundColor = [UIColor redColor];
    lab.backgroundColor=[UIColor clearColor];
    [btn addSubview:lab];
    [lab release];
    
    NSArray *arr =[[NSArray alloc] initWithObjects:NSLocalizedString(@"listTitle2", nil),NSLocalizedString(@"starItem", nil),NSLocalizedString(@"starItem1", nil),NSLocalizedString(@"starItem2", nil),NSLocalizedString(@"starItem3", nil),NSLocalizedString(@"starItem4", nil),NSLocalizedString(@"starItem5", nil),NSLocalizedString(@"starItem6", nil),NSLocalizedString(@"starItem7", nil),NSLocalizedString(@"starItem8", nil),NSLocalizedString(@"starItem9", nil),NSLocalizedString(@"starItem10", nil),NSLocalizedString(@"starItem11", nil),nil];
    if([dic count])
    {
        selectStar=[[dic valueForKey:@"star"] intValue];
        
    }else{
        selectStar=0;
    }
    CGSize allSize = [[NSString stringWithFormat:@"%@ ->",[arr objectAtIndex:selectStar]] sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(1000, 30)];
    starLabel = [[UILabel alloc]initWithFrame:CGRectMake(290-10-allSize.width, 10, allSize.width, 30)];
    starLabel.font=[UIFont systemFontOfSize:16];
    starLabel.text= [NSString stringWithFormat:@"%@ ->",[arr objectAtIndex:selectStar]];
    starLabel.backgroundColor=[UIColor clearColor];
    [btn addSubview:starLabel];
    [starLabel release];
    [arr release];
//    UIView *lineView = [[UIView alloc] init];
//    lineView.backgroundColor = [UIColor lightGrayColor];
//    lineView.frame = CGRectMake(0, 50, 290, 1);
//    [btn addSubview:lineView];
    btn.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    btn.layer.borderWidth  = 2.0;
    btn.layer.cornerRadius = 5;
    
    [btn addTarget:self action:@selector(starSearchChange) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [self.view addSubview:btn];
    
    
    
    BanBu_NavButton *button=[[BanBu_NavButton  alloc] initWithButtonType:UIButtonCreatTypeCustomBtn];
    button.frame=CGRectMake(15, 350, 290, 45);
    [button addTarget:self action:@selector(changeToSelect:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:NSLocalizedString(@"confirmNotice", nil) forState:UIControlStateNormal];
    
    [self.view addSubview:button];

    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
    {
        BanBu_ApplyVipViewController *aplyVip = [[BanBu_ApplyVipViewController alloc] init];
        [self.navigationController pushViewController:aplyVip animated:YES];
        [aplyVip release];
    }
}
-(void)noticeVip
{

    [[BanBu_VIPAlert shareInstance] GoVIPApplyViewAlertFor:self title:NSLocalizedString(@"sysNotice", nil) message:NSLocalizedString(@"vipCanUse", nil)];
}
-(void)changeToSelect:(UIButton *)sender
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];

    [dic setValue:[NSNumber numberWithInteger:segMent.selectedIndex] forKey:@"sex"];
    
    [dic setValue:[NSNumber numberWithInteger:segMentTime.selectedIndex] forKey:@"time"];
    [dic setValue:[NSNumber numberWithInteger:selectAge] forKey:@"age"];
    [dic setValue:[NSNumber numberWithInteger:selectStar] forKey:@"star"];
    
    
    [UserDefaults setValue:dic forKey:[NSString stringWithFormat:@"%@electNearBy",MyAppDataManager.useruid]];
    
      [[NSNotificationCenter defaultCenter]postNotificationName:@"toselect" object:dic];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        
        NSData *listData = [NSKeyedArchiver archivedDataWithRootObject:dic];
      
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@electNearBy",MyAppDataManager.useruid]];
        [listData writeToFile:path atomically:YES];
       
      
    });

     [self dismissViewControllerAnimated:YES];
    
    [dic release];


}
-(void)toseleSex:(SVSegmentedControl *)sender
{
    selectsex=sender.selectedIndex;
}

-(void)toseleTime:(SVSegmentedControl*)sender
{
    selectTime=sender.selectedIndex;
}


-(void)toSelectAge:(SVSegmentedControl*)sender
{
    selectAge = sender.selectedIndex;
}


-(void)toSelsect:(UIButton *)sender
{

    [self.navigationController dismissViewControllerAnimated:YES];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
-(void)starSearchChange
{
    if([[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"userlevel"] integerValue]>0)
    {
        BanBu_StarSearchViewController *star = [[BanBu_StarSearchViewController alloc] initWithSelectIndex:selectStar];
        star.delegate = self;
        BanBu_NavigationController *nav=[[[BanBu_NavigationController alloc]initWithRootViewController:star] autorelease];
        nav.modalPresentationStyle=UIModalPresentationPageSheet;
        [self  presentViewController:nav animated:YES];
        [star release];
    }
    else
    {
        [[BanBu_VIPAlert shareInstance] GoVIPApplyViewAlertFor:self title:NSLocalizedString(@"sysNotice", nil) message:NSLocalizedString(@"vipCanUse", nil)];
    }

    
}
-(void)confirmSelectStarIndex:(NSInteger)_selectStar
{
    NSArray *arr =[[NSArray alloc] initWithObjects:NSLocalizedString(@"listTitle2", nil),NSLocalizedString(@"starItem", nil),NSLocalizedString(@"starItem1", nil),NSLocalizedString(@"starItem2", nil),NSLocalizedString(@"starItem3", nil),NSLocalizedString(@"starItem4", nil),NSLocalizedString(@"starItem5", nil),NSLocalizedString(@"starItem6", nil),NSLocalizedString(@"starItem7", nil),NSLocalizedString(@"starItem8", nil),NSLocalizedString(@"starItem9", nil),NSLocalizedString(@"starItem10", nil),NSLocalizedString(@"starItem11", nil),nil];
    selectStar = _selectStar;
    starLabel.text = [NSString stringWithFormat:@"%@ ->",[arr objectAtIndex:_selectStar]];
    CGSize allSize = [starLabel.text sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(1000, 30)];
    starLabel.frame = CGRectMake(290-10-allSize.width, 10, allSize.width, 30);
    [arr release];

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
